/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.toto.product.Product;
import com.toto.product.ProductHome;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 *
 * @author Administrator
 */
public class EntityBeanClient
{

  public static void main(String[] args) throws RemoveException
  {
    ProductHome productHome = null;
    try
    {
      Context ctx = getInitialContext();
      Object obj = ctx.lookup("ejb/product");
      productHome = (ProductHome) PortableRemoteObject.narrow(obj, ProductHome.class);
      Product product1 = productHome.create("A0001", "tuzi", "tuzi", 20);
      Product product2 = productHome.create("B0002", "xiaomao", "xiaomao", 30);
      Product product3 = productHome.create("C0003", "xiaogou", "xiaogou", 50);
      Product product4 = productHome.create("D0004", "zhu", "benzhu", 40);

      Iterator iterator = productHome.findByName("tuzi").iterator();
      while(iterator.hasNext())
      {
        Product product = (Product) PortableRemoteObject.narrow(iterator.next(), Product.class);
        System.out.println(product.getDescription());
      }
      /*
       * 找出价钱为20的商品
       */
      System.out.println("Finding all products that cost 20");
      iterator = productHome.findByBasePrice(20).iterator();
      while(iterator.hasNext())
      {
        Product product = (Product) PortableRemoteObject.narrow(iterator.next(), Product.class);
        System.out.println(product.getDescription());
      }
    }
    catch (FinderException ex)
    {
      Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (RemoteException ex)
    {
      Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (CreateException ex)
    {
      Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (NamingException ex)
    {
      Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally
    {
      if (productHome != null)
      {
        try
        {
          System.out.println("Destorying products..");
          /*
           *查出所有商品
           */
          Iterator iterator = productHome.findAllProduct().iterator();
          while (iterator.hasNext())
          {
            Product product = (Product) PortableRemoteObject.narrow(iterator.next(), Product.class);
            System.out.println(product.getName());
            if (product.getProductID().startsWith("A"))
            {
              product.remove();
            }
          }
        }
        catch (FinderException ex)
        {
          Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (RemoteException ex)
        {
          Logger.getLogger(EntityBeanClient.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }
  }
  public static InitialContext getInitialContext() throws NamingException
  {
    Properties p = new Properties();
    p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    p.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
    return new InitialContext(p);
  }
}
